package org.dromara.web.controller.wechat;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerListResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.wechat.service.WechatOpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/wechat/api")
@Validated
@RequiredArgsConstructor
@RestController
@Slf4j
public class WechatApiController {
    @Autowired
    private WechatOpenService wechatOpenService;

    @GetMapping("/auth/goto_auth_url_show")
    @ResponseBody
    public String gotoPreAuthUrlShow(){
        return "<a href='goto_auth_url'>go</a>";
    }

    @GetMapping("/auth/goto_auth_url")
    public R gotoPreAuthUrl(HttpServletRequest request){
        String host = request.getHeader("host");
        log.info("host:", host);
        String url = "http://"+host+"/dev-api/wechat/api/auth/jump";
        try {
            url = wechatOpenService.getWxOpenComponentService().getPreAuthUrl(url);
            log.info("gotoPreAuthUrl:{}", url);
            Map<String, String> map = new HashMap<>();
            map.put("url", url);
            return R.ok(map);
        } catch (Exception e) {
            log.error("gotoPreAuthUrl", e);
            throw new ServiceException(e.getMessage());
        }
    }


    @GetMapping("/auth/jump")
    @ResponseBody
    public WxOpenQueryAuthResult jump(@RequestParam("auth_code") String authorizationCode){
        try {
            WxOpenQueryAuthResult queryAuthResult = wechatOpenService.getWxOpenComponentService().getQueryAuth(authorizationCode);
            log.info("getQueryAuth:{}", queryAuthResult);
            return queryAuthResult;
        } catch (WxErrorException e) {
            log.error("gotoPreAuthUrl", e);
            throw new ServiceException(e.getMessage());
        }
    }

    @GetMapping("/getAuthorizerList")
    @ResponseBody
    public WxOpenAuthorizerListResult getAuthorizerList(){
        try {
            return wechatOpenService.getWxOpenComponentService().getAuthorizerList(0,500);
        } catch (WxErrorException e) {
            log.error("getAuthorizerList", e);
            throw new ServiceException(e.getMessage());
        }
    }

    @GetMapping("/get_authorizer_info")
    @ResponseBody
    public WxOpenAuthorizerInfoResult getAuthorizerInfo(@RequestParam String appId){
        try {
            return wechatOpenService.getWxOpenComponentService().getAuthorizerInfo(appId);
        } catch (WxErrorException e) {
            log.error("getAuthorizerInfo", e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 启动票据推送服务
     */
    @PostMapping("/start_push_ticket")
    public void startPushTicket(){
        try {
            wechatOpenService.startPushTicket();
        } catch (WxErrorException e) {
            log.error("startPushTicket", e);
            throw new ServiceException(e.getMessage());
        }
    }

//    /**
//     * 获取授权方选项信息
//     */
//    @GetMapping("/get_authorizer_option_info")
//    @ResponseBody
//    public WxOpenAuthorizerOptionResult getAuthorizerOptionInfo(@RequestParam String authorizerAppid, @RequestParam String optionName){
//        try {
//            return wechatOpenService.getAuthorizerOptionInfo(authorizerAppid, optionName);
//        } catch (WxErrorException e) {
//            log.error("getAuthorizerOptionInfo", e);
//            throw new ServiceException(e.getMessage());
//        }
//    }

    /**
     * 查询rid信息
     */
    @GetMapping("/getRidInfo")
    @ResponseBody
    @RepeatSubmit
    public R<Boolean> getRidInfo(@RequestParam("rid") String rid) throws WxErrorException {
        return R.ok(wechatOpenService.getRidInfo(rid));
    }

    /**
     * 重置API调用次数
     */
    @PostMapping("/clearQuota")
    @ResponseBody
    public R<Boolean> clearQuota() {
        return R.ok(wechatOpenService.clearQuota());
    }
}
